package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserAuthSmsService;
import com.fqgj.xjd.user.client.enums.UserAuthSmsTypeEnum;
import com.fqgj.xjd.user.client.request.UserAuthSms;
import com.fqgj.xjd.user.client.request.UserAuthSmsHistory;
import com.fqgj.xjd.user.client.response.UserAuthSmsInfo;
import com.fqgj.xjd.user.common.enums.UserErrorCodeEnum;
import com.fqgj.xjd.user.dao.UserAuthSmsDao;
import com.fqgj.xjd.user.dao.UserAuthSmsHistoryDao;
import com.fqgj.xjd.user.entity.UserAuthSmsEntity;
import com.fqgj.xjd.user.entity.UserAuthSmsHistoryEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: cashmama-user
 * @description
 * @author: frank
 * @create: 2020-01-14 17:06
 **/
@Service("userAuthSmsService")
public class UserAuthSmsServiceImpl implements UserAuthSmsService {

    @Autowired
    private UserAuthSmsDao userAuthSmsDao;
    @Autowired
    private UserAuthSmsHistoryDao userAuthSmsHistoryDao;

    @Override
    public Response<Boolean> addUserAuthSms(UserAuthSms userAuthSms) {
        if (userAuthSms == null || StringUtils.isBlank(userAuthSms.getUserCode())
                || userAuthSms.getType() == null || userAuthSms.getStatus() == null) {
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }

        UserAuthSmsEntity userAuthSmsEntity = userAuthSmsDao.selectUserAuthSmsByUserCode(userAuthSms.getUserCode(), userAuthSms.getType());
        if(userAuthSmsEntity != null){
            UserAuthSmsEntity entity =  new UserAuthSmsEntity();
            BeanUtils.copyProperties(userAuthSms, entity);
            entity.setId(userAuthSmsEntity.getId());
            userAuthSmsDao.updateByPrimaryKey(entity);
        }else{
            UserAuthSmsEntity entity = new UserAuthSmsEntity();
            BeanUtils.copyProperties(userAuthSms, entity);
            userAuthSmsDao.insert(entity);
        }
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> addUserAuthSmsHistory(UserAuthSmsHistory userAuthSmsHistory) {
        if(StringUtils.isBlank(userAuthSmsHistory.getUserCode())
                || userAuthSmsHistory.getType() == null || userAuthSmsHistory.getStatus() == null){
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }
        UserAuthSmsHistoryEntity userAuthSmsHistoryEntity = new UserAuthSmsHistoryEntity();
        BeanUtils.copyProperties(userAuthSmsHistory, userAuthSmsHistoryEntity);
        userAuthSmsHistoryDao.insert(userAuthSmsHistoryEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<UserAuthSmsInfo> getUserAuthSmsInfo(String userCode, UserAuthSmsTypeEnum type) {
        if(StringUtils.isEmpty(userCode) || type == null){
            return Response.error(UserErrorCodeEnum.PARAM_ERROR);
        }
        UserAuthSmsEntity userAuthSmsEntity = userAuthSmsDao.selectUserAuthSmsByUserCode(userCode, type.getType());
        if(userAuthSmsEntity == null){
            return Response.ok().putData(null);
        }
        UserAuthSmsInfo userAuthSmsInfo = new UserAuthSmsInfo();
        BeanUtils.copyProperties(userAuthSmsEntity, userAuthSmsInfo);
        return Response.ok().putData(userAuthSmsInfo);
    }
}
